//
//  SCNMainViewController.swift
//  ShoucainuSwift
//
//  Created by 王家旭 on 2017/2/6.
//  Copyright © 2017年 Riluee. All rights reserved.
//

import UIKit

class SCNMainViewController: UITabBarController,UITabBarControllerDelegate {
    
    var homeVC : SCNHomeViewController!
    var investmentVC : SCNInvestmentViewController!
    var borrowVC : SCNBorrowViewController!
    var centerVC : SCNCenterViewController!
    var moreVC : SCNMoreViewController!
    
    
    

    override func viewDidLoad() {
        super.viewDidLoad()

        self.delegate = self

        setTabbar()
        
        self.tabBarItem.badgeColor = SCNConstants.colorWith255RGBA(10, g: 10, b: 10, a: 1)

        self.view.backgroundColor = SCNConstants.colorFromHexValue(0x101010, alpha: 1)

    }


    func setTabbar()  {
        
        var nav : UINavigationController!
        
        
        // 首页
        
        if homeVC == nil {
            
            homeVC = SCNHomeViewController()
            nav = UINavigationController(rootViewController:homeVC)
            nav.title = "首页"
            nav.tabBarItem.image = UIImage(named: "home_icon_normal")
            nav.tabBarItem.selectedImage = UIImage(named: "home_icon_select")
            self.addChildViewController(nav)
            
            
        }
        
        // 我要投资
        
        if investmentVC == nil {
            
            investmentVC = SCNInvestmentViewController()
            
            nav = UINavigationController(rootViewController:investmentVC)
            nav.title = "我要投资"
            nav.tabBarItem.image = UIImage(named: "account_icon_normal")
            nav.tabBarItem.selectedImage = UIImage(named: "account_icon_select")
            self.addChildViewController(nav)
            
            
        }
        
        // 我要借款
        
        if borrowVC == nil {
            
            borrowVC = SCNBorrowViewController()
            
            nav = UINavigationController(rootViewController:borrowVC)
            nav.title = "我要借款"
            nav.tabBarItem.image = UIImage(named: "jiekuan_icon_normal")
            nav.tabBarItem.selectedImage = UIImage(named: "jiekuan_icon_select")

            self.addChildViewController(nav)
            
            
        }
        
        // 账户中心
        
        if centerVC == nil {
            
            centerVC = SCNCenterViewController()
            
            nav = UINavigationController(rootViewController:centerVC)
            nav.title = "账户中心"
            nav.tabBarItem.image = UIImage(named: "user_icon_normal")
            nav.tabBarItem.selectedImage = UIImage(named: "user_icon_select")
            self.addChildViewController(nav)
            
            
        }
        
        // 更多
        
        if moreVC == nil {
            
            moreVC = SCNMoreViewController()
            nav = UINavigationController(rootViewController:moreVC)
            nav.title = "更多"
            nav.tabBarItem.image = UIImage(named: "more_icon_normal")
            nav.tabBarItem.selectedImage = UIImage(named: "more_icon_select")
            self.addChildViewController(nav)
            
            
        }
    }
    
    // MARK: - UITabBarController delegate
    
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        
        /**
         *  双击TabItem
         */
//        let taSelectViewController = tabBarController.selectedViewController
//        
//        if taSelectViewController!.isEqual(viewController) {
//            
//            let navViewController = viewController as! UINavigationController
//            let rootViewController = navViewController.viewControllers[0]
//            
//            if rootViewController.isKind(of: SCNHomeViewController.classForCoder()){
//                
//                
//                
//            }
//            
//        }
        
        
        let index = tabBarController.viewControllers?.index(of: viewController)
        
        if index == 3 && !SCNSQLiteManager().isLogin() {
            let loginvc = LoginViewController()
            loginvc.loginAction = {
                self.selectedIndex = 3
            }
            let nav = UINavigationController(rootViewController: loginvc)
            
            self.present(nav, animated: true, completion: nil)
            return false
        }
        
        return true
    }

    
    
    
}
